package ead.cs.service.impl;

import ead.cs.domain.Payment;
import ead.cs.repository.DeclarationRepository;
import ead.cs.repository.PaymentRepository;
import ead.cs.service.DeclarationService;
import ead.cs.service.PaymentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService {

    private static final Logger LOGGER = Logger.getLogger(PaymentServiceImpl.class);

    @Autowired
    private PaymentRepository paymentRepository;
    @Autowired
    private DeclarationService declarationService;

    @Override
    public void save(Payment payment) {
        LOGGER.debug("saving payment: " + payment);
        if (payment == null || payment.getDeclaration() == null) {
            throw new RuntimeException("Invalid Payment instance: " + payment);
        }
        declarationService.save(payment.getDeclaration());
        paymentRepository.save(payment);
    }

    @Override
    public Payment findOne(String id) {
        return paymentRepository.findOne(id);
    }

}
